package com.ai.manager.edu.cms.user.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.ai.common.util.KeysEnum;
import com.ai.common.utils.CommonUtil;
import com.ai.common.utils.cache.redis.shared.RedisUtils;
import com.ai.dao.edu.cms.user.OperatorRoleMapper;
import com.ai.manager.edu.cms.user.OperatorRoleManager;
import com.ai.model.edu.cms.backoperator.OperatorRole;
import com.ai.model.edu.cms.backoperator.dto.OperatorRoleDTO;
import com.ai.model.edu.cms.backoperator.vo.OperatorCountVO;
import com.ai.model.edu.cms.backoperator.vo.OperatorRoleVO;

/**
 * Class Name : UserRoleManagerImpl<br>
 * 
 * Description : 用户角色关系manager实现<br>
 * 
 * @author yangwb
 * @version $Revision$
 * @see
 *
 */
@Component("operatorRoleManager")
public class OperatorRoleManagerImpl implements OperatorRoleManager {
	
	private Log log = LogFactory.getLog(getClass());
    @Resource
    private OperatorRoleMapper operatorRoleMapper;
    
    @Resource 
    private RedisUtils redisUtils;

    /**
     * Description ： 根据dto查询用户角色关系<br>
     * 
     * yangwb
     * 
     * @param userRoleDTO
     * @return
     * @since
     * 
     */
    public List<OperatorRoleVO> findUserRoleVOsByDTO(OperatorRoleDTO userRoleDTO) {
        return operatorRoleMapper.findUserRoleVOsByDTO(userRoleDTO);
    }

    /**
     * Description ： 批量新增角色/用户关系
     * 
     * nyj
     * 
     * @param userRoles
     * @since
     *
     */
    public void addUserRoleBatch(List<OperatorRole> userRoles) {
    	
    	try {
    		List<Long> backOperatorIds = CommonUtil.getValueList(userRoles, "backOperatorId");
        	if(backOperatorIds != null &&  backOperatorIds.size() > 0){
        		for(Long backOperator : backOperatorIds){
        			String key = KeysEnum.BACKEND_MENU_FUNCTION.getKey() + "_" + backOperator;
        			redisUtils.del(key);
        		}
        	}
		} catch (Exception e) {
			log.error("删除用户角色缓存异常" , e);
		}
    	
        operatorRoleMapper.addUserRoleBatch(userRoles);
    }

    /**
     * Description ： 根据backendOperatorId删除用户角色关系<br>
     * 
     * yangwb
     * 
     * @param backendOperatorId
     * @return
     * @since
     *
     */
    public int deleteUserRoleByBackOperatorId(Long backOperatorId) {
    	try {
    		String key = KeysEnum.BACKEND_MENU_FUNCTION.getKey() + "_" + backOperatorId;
    		redisUtils.del(key);
		} catch (Exception e) {
			log.error("删除用户角色缓存异常" , e);
		}
    	
        return operatorRoleMapper.deleteUserRoleByBackOperatorId(backOperatorId);
    }

    /**
     * Description ：新增角色角色用户关系
     * 
     * yangwb
     * 
     * @param OperatorRole operatorRole
     * @return
     * @since
     *
     */
    public void insert(OperatorRole operatorRole) {
    	try {
    		String key = KeysEnum.BACKEND_MENU_FUNCTION.getKey() + "_" + operatorRole.getBackOperatorId();
    		redisUtils.del(key);
		} catch (Exception e) {
			log.error("删除用户角色缓存异常" , e);
		}
        operatorRoleMapper.insert(operatorRole);
    }

    /**
     * Description ：根据ID删除
     * 
     * yangwb
     * 
     * @param Long id
     * @return
     * @since
     *
     */
    public void deleteById(Long id) {
        operatorRoleMapper.deleteById(id);
    }

    /**
     * Description ： 根据用户ID和角色ID删除用户角色关系
     * 
     * nyj
     * 
     * @param roleId
     * @param backOperatorIds
     * @return
     * @since
     *
     */
    public int deleteOperatorRoleByRoleIdAndOperatorId(Long roleId, List<Long> operatorIds) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleId", roleId);
        map.put("operatorIds", operatorIds);
        try {
        	for(Long operatorId : operatorIds){
        		String key = KeysEnum.BACKEND_MENU_FUNCTION.getKey() + "_" + operatorId;
        		redisUtils.del(key);
        	}
		} catch (Exception e) {
			log.error("删除用户角色缓存异常" , e);
		}
        return operatorRoleMapper.deleteOperatorRoleByRoleIdAndOperatorId(map);
    }

    /**
     * Description ：根据用户id 和角色ids 删除关系<br>
     * 
     * yangwb
     * 
     * @param backOperatorId
     * @param roleIds
     * @return
     * @since
     * 
     */
    public int removeOperatorRoleByRoleIdsAndOperatorId(Long backOperatorId, List<Long> roleIds) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("backOperatorId", backOperatorId);
        map.put("roleIds", roleIds);
        try {
        		String key = KeysEnum.BACKEND_MENU_FUNCTION.getKey() + "_" + backOperatorId;
        		redisUtils.del(key);
		} catch (Exception e) {
			log.error("删除用户角色缓存异常" , e);
		}
        return operatorRoleMapper.removeOperatorRoleByRoleIdsAndOperatorId(map);
    }

    /**
     * Description ： 根据角色统计用户数量
     * 
     * nyj
     * 
     * @param roleId
     * @return
     * @since
     *
     */
    public List<OperatorCountVO> countOperatorByRoleIds(List<Long> roleIds) {
        return operatorRoleMapper.countOperatorByRoleIds(roleIds);
    }

    /**
     * Description ： 根据角色ID删除用户角色关系
     * 
     * nyj
     * 
     * @param roleId
     * @return
     * @since
     *
     */
    public Integer deleteOperatorRoleByRoleId(Long roleId) {
        return operatorRoleMapper.deleteOperatorRoleByRoleId(roleId);
    }

    /**
     * Description ： 根据主键id 查询用户角色关系<br>
     * 
     * yangwb
     * 
     * @param id
     * @return
     * @since
     *
     */
    public OperatorRoleVO selectByPrimaryKey(Long id) {
        return operatorRoleMapper.selectByPrimaryKey(id);
    }
}
